package org.finesys.ai.service.impl;

import org.finesys.ai.entity.AigcApp;
import org.finesys.ai.entity.AigcAppApi;
import org.finesys.ai.mapper.AigcAppApiMapper;
import org.finesys.ai.service.AigcAppApiService;
import org.finesys.ai.service.AigcAppService;
import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class AigcAppApiServiceImpl extends BaseServiceImpl<AigcAppApiMapper, AigcAppApi> implements AigcAppApiService {

    private final AigcAppService aigcAppService;


    /**
     * 根据ID获取AigcAppApi实例
     *
     * @param id 要获取的AigcAppApi实例的ID
     * @return 返回AigcAppApi实例，如果未找到则返回null
     */
    @Override
    public AigcAppApi getById(String id) {
        AigcAppApi aigcAppApi = super.getById(id);
        if (aigcAppApi != null && StringUtils.hasText(aigcAppApi.getAppId())) {
            AigcApp aigcApp = aigcAppService.getById(aigcAppApi.getAppId());
            aigcAppApi.setApp(aigcApp);
        }
        return aigcAppApi;
    }

    @Override
    public AigcAppApi create(AigcAppApi aigcAppApi){
        String apiKey=IdUtil.fastSimpleUUID();
        aigcAppApi.setApiKey(apiKey);
        super.save(aigcAppApi);
        return aigcAppApi;
    }
}
